package net.haiyue.api.repository;
import net.haiyue.api.entity.Event;
import net.haiyue.api.entity.SelectInfo;
import net.haiyue.api.entity.viewModel.EventViewModel;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Transactional
public interface EventRepository extends BaseRepository<Event,Integer> {
    @Query( value = "select * from event where id=?1",nativeQuery = true)
    Event findEventById(long  id);

    @Query(value ="SELECT DISTINCT M.name AS label,M.id As value FROM  modules M  LEFT JOIN rmu R ON M.id=R.mid LEFT " +
            "join " +
            "user u on R.uid=u.id where u.email=?1 and M.pid=?2",nativeQuery = true)
    List<SelectInfo>selectModule(String email,Integer id);

    @Query(value ="select e.*, m.name as moduleName,p" +
            ".name as projectName from event e left join " +
            "rmu r on e.raid=r.id left join modules m on " +
            "r.mid=m.id LEFT join projects p on m.pid=p" +
            ".id LEFT JOIN user u on r.uid=u.id" +
            " where if(?1 is not null,p.id=?1,1=1) and if(?2 is not null,m.id=?2,1=1) and e.create_time between ?3" +
            " and ?4 and if(?5 is not null,u.email=?5,1=1)",
            nativeQuery = true)
    List<EventViewModel>all(Integer pid, Integer mid,Date time_begin,Date time_end,String email);
}
